package com.sys.menu.service.impl;

import com.app.view.TreeMenu;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.menu.entity.SysMenu;
import com.sys.menu.mapper.SysMenuMapper;
import com.sys.menu.service.SysMenuService;
import com.sys.menu.view.SysMenuView;
import com.sys.user.entity.SysUser;
import com.sys.user.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author djianfeng
 * @since 2018-07-24
 */
@Transactional
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    SysUserService userService;
    @Autowired
    SysMenuMapper menuMapper;

    /**
     * 获取用户菜单
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Set<String> findMenusByUserId(Long userId) {
        Map map = new HashMap();
        SysUser user = this.userService.selectById(userId);
        if (StringUtils.isBlank(user.getUserType()) || !user.getUserType().equals("0")) {
            List<String> codes = menuMapper.selectCodeByUserId(userId);
            return new HashSet<>(codes);
        }
        //超管
        List<SysMenu> list = (List)this.selectByMap(map);
        Set<String> set = new HashSet();
        for (SysMenu menu : list) {
            set.add(menu.getCode());
        }
        return set;
    }

    @Override
    public List<TreeMenu> selectTreeMenuByUserId(Long userId) {
        List<TreeMenu> treeMenus = new ArrayList<>();
        SysUser user = this.userService.selectById(userId);
        //超管
        List<SysMenu> menus;
        if (StringUtils.isNotBlank(user.getUserType()) && user.getUserType().equals("0")) {
            menus = (List)this.selectByMap(new HashMap());
        } else {
            menus = this.menuMapper.selectMenuByUserId(userId);
        }
        this.convertMenu(treeMenus, menus);
        return treeMenus;
    }

    private void convertMenu(List<TreeMenu> treeMenus, List<SysMenu> menus) {
        if (menus == null || menus.isEmpty()) {
            return;
        }
        for (int i = 0; i < menus.size(); i++) {
            //一级菜单
            SysMenu menu1 = menus.get(i);
            if (menu1.getPid() == null && menu1.getType() == 1) {
                TreeMenu treeMenu = new TreeMenu();
                treeMenu.setSysMenu(menu1);
                for (int j = 0; j < menus.size(); j++) {
                    //二级菜单
                    SysMenu menu2 = menus.get(j);
                    if (menu2.getPid() != null && menu1.getId().equals(menu2.getPid()) && menu2.getType() == 1) {
                        TreeMenu treeMenu2 = new TreeMenu();
                        treeMenu2.setSysMenu(menu2);
                        for (int k = 0; k < menus.size(); k++) {
                            //三级菜单（按钮）
                            SysMenu menu3 = menus.get(k);
                            if (menu3.getPid() != null && menu2.getId().equals(menu3.getPid()) && menu3.getType() == 2) {
                                TreeMenu treeMenu3 = new TreeMenu();
                                treeMenu3.setSysMenu(menu3);
                                treeMenu2.getChildren().add(treeMenu3);
                            }
                        }
                        treeMenu.getChildren().add(treeMenu2);
                    }
                }
                treeMenus.add(treeMenu);
            }
        }
    }

    private void convertAllMenu(List<TreeMenu> treeMenus, List<SysMenu> menus) {
        if (menus == null || menus.isEmpty()) {
            return;
        }
        for (int i = 0; i < menus.size(); i++) {
            //一级菜单
            SysMenu menu1 = menus.get(i);
            if (menu1.getPid() == null && menu1.getType() == 1) {
                TreeMenu treeMenu = new TreeMenu();
                treeMenu.setSysMenu(menu1);
                for (int j = 0; j < menus.size(); j++) {
                    //二级菜单
                    SysMenu menu2 = menus.get(j);
                    if (menu2.getPid() != null && menu1.getId().equals(menu2.getPid())) {
                        TreeMenu treeMenu2 = new TreeMenu();
                        treeMenu2.setSysMenu(menu2);
                        for (int k = 0; k < menus.size(); k++) {
                            //三级菜单（按钮）
                            SysMenu menu3 = menus.get(k);
                            if (menu3.getPid() != null && menu2.getId().equals(menu3.getPid())) {
                                TreeMenu treeMenu3 = new TreeMenu();
                                treeMenu3.setSysMenu(menu3);
                                treeMenu2.getChildren().add(treeMenu3);
                            }
                        }
                        treeMenu.getChildren().add(treeMenu2);
                    }
                }
                treeMenus.add(treeMenu);
            }
        }
    }

    @Override
    public List<SysMenuView> getSysMenuViews(Page<SysMenuView> page, SysMenuView view) {
        page.setRecords(menuMapper.getSysMenuViews(page,view));
        return page.getRecords();
    }

    @Override
    public List<TreeMenu> selectTreeMenu() {
        List<SysMenu> menus= (List)this.selectByMap(new HashMap());
        List<TreeMenu> treeMenus = new ArrayList<>();
        this.convertMenu(treeMenus, menus);
        return treeMenus;
    }

    @Override
    public List<TreeMenu> selectAllMenu() {
        List<SysMenu> menus= (List)this.selectByMap(new HashMap());
        List<TreeMenu> treeMenus = new ArrayList<>();
        this.convertAllMenu(treeMenus, menus);
        return treeMenus;
    }
}
